package com.salim.leetcode.$1306;

public class JumpGameIII {
    private static int[] array;
    private static boolean[] jumped;
    public boolean canReach(int[] arr, int start) {
        array = arr;
        jumped = new boolean[arr.length];
        return doReach(start);
    }

    public boolean doReach(int start){
        if(start+array[start]<array.length){
            if(!jumped[array[start]+start]){
                jumped[array[start]+start] = true;
                if(array[array[start]+start]==0 || doReach(array[start]+start)){
                    return true;
                }
            }
        }
        if(start-array[start]>=0){
            if(!jumped[start-array[start]]){
                jumped[start-array[start]] = true;
                if(array[start-array[start]]==0 || doReach(start-array[start])){
                    return true;
                }
            }
        }
        return false;
    }
}
